package wca;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.snapshot.ITurnSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * TestChangeBehavior tests if the robot is able to change behavior depending on the target's
 * behavior.
 * 
 * @author Calvin Wong
 * 
 */

public class TestChangeBehavior extends RobotTestBed {

  private boolean visitedTopLeft = false;
  private boolean visitedTopRight = false;
  private boolean visitedBottomLeft = false;
  private boolean visitedBottomRight = false;

  /**
   * Tests if the robot wins against the enemy robot.
   * 
   * @return 10 rounds
   */
  public int getNumRounds() {
    return 10;
  }

  /**
   * Determine if the robot has changed behaviors.
   * 
   * @param e Turn Ended Event
   * 
   */
  @Override
  public void onTurnEnded(TurnEndedEvent e) {

    // Get a snapshot of the turn.
    ITurnSnapshot snapshot = e.getTurnSnapshot();

    // Get all the active robots.
    IRobotSnapshot[] robots = snapshot.getRobots();

    if (robots[1].getX() == 800 - 18 && robots[1].getY() == 600 - 18) {
      visitedTopRight = true;
    }

    if (robots[1].getX() == 18 && robots[1].getY() == 600 - 18) {
      visitedTopLeft = true;
    }

    if (robots[1].getX() == 18 && robots[1].getY() == 18) {
      visitedBottomLeft = true;
    }

    if (robots[1].getX() == 800 - 18 && robots[1].getY() == 18) {
      visitedBottomRight = true;
    }
  }

  /**
   * Check of the robot has changed behaviors and won the battle.
   * 
   * @param e Battle Completed Event
   * 
   */
  public void onBattleCompleted(BattleCompletedEvent e) {
    BattleResults[] results = e.getIndexedResults();
    BattleResults chinpoGumResults = results[1];
    String robotName = chinpoGumResults.getTeamLeaderName();

    assertEquals("Check that the winner is ChinpoGum", robotName, "wca.ChinpoGum*");

    assertTrue("Check that the robot has visited the top left corner", visitedTopLeft);
    assertTrue("Check that the robot has visited the top right corner", visitedTopRight);
    assertTrue("Check that the robot has visited the bottom left corner", visitedBottomLeft);
    assertTrue("Check that the robot has visited the bottom right corner", visitedBottomRight);
  }

  /**
   * Robots to be used for this test.
   * 
   * @return Robots
   * 
   */
  @Override
  public String getRobotNames() {
    return "sample.Walls,wca.ChinpoGum";
  }
}
